package facade;

import java.awt.event.ActionListener;

import java.util.Observable;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import model.TextModel;

import controler.LoginListener;

import template.AbstractSystem;
import view.LoginLabel;

public class LoginSystem extends AbstractSystem{

	private static final long serialVersionUID = 1L;
	private JLabel loginLabel;
	private JTextField usernameField;
	private JPasswordField passwordField;
	private ActionListener loginListener;
	
	public static void main(String[] args){
		new LoginSystem();
	}
	
	public LoginSystem(){
		setCurrentSystem(this);
		Observable textModel = new TextModel("ENTER YOUR NAME AND USERNAME");
		
		JPanel loginPanel = createLoginPanel();
		loginLabel = createLoginLabel(textModel);
		usernameField = createTextField();
		passwordField = createPasswordField();
		JButton loginButton = createLoginButton();
		JButton cancelButton = createCancelButton();
		
		loginListener = new LoginListener(textModel, usernameField, passwordField);
		loginButton.addActionListener(loginListener);
		
		cancelButton.addActionListener(new ExitProgramListener());
		
		loginPanel.add(loginLabel);
		loginPanel.add(usernameField);
		loginPanel.add(passwordField);
		loginPanel.add(loginButton);
		loginPanel.add(cancelButton);
		
		addWindowListener(new CloseSystemListener());
		
		setContentPane(loginPanel);
		setBounds((int)dim.getWidth()/2-250, (int)dim.getHeight()/2-80, 500, 160);
		setTitle("Login");
		setLayout(null);
		setUndecorated(true);
		setVisible(true);
	}
	
	private JPanel createLoginPanel(){
		JPanel panel = new JPanel();
		return panel;
	}
		
	private JLabel createLoginLabel(Observable textModel){ 
		JLabel label = new LoginLabel(textModel, "ENTER YOUR NAME AND USERNAME");
		return label;
	}
	
	private JTextField createTextField(){
		JTextField field = new JTextField();
		field.setBounds(500/2-150, 50, 300, 30);
		return field;
	}
	
	private JPasswordField createPasswordField(){
		JPasswordField field = new JPasswordField();
		field.setBounds(500/2-150, 80, 300, 30);
		return field;
	}
	
	private JButton createCancelButton(){
		JButton cancel = new JButton("Cancel");
		cancel.setBounds(170, 110, 80, 30);
		return cancel;
	}
	
	private JButton createLoginButton(){
		JButton login = new JButton("Login");
		login.setBounds(250, 110, 80, 30);
		return login;
	}
}
